package com.hjy.cabinet.db;

import android.util.Log;

import com.hjy.cabinet.db.domain.BaseConfig;

import java.util.ArrayList;
import java.util.List;

public class BaseConfigInstance {
    private static BaseConfigInstance instance;

    private BaseConfigHelper dbHelper;

    private BaseConfigInstance() {
    }

    public static BaseConfigInstance getInstance() {
        if (instance == null) {
            synchronized (BaseConfigInstance.class) {
                if (instance == null) {
                    instance = new BaseConfigInstance();
                }
            }
        }
        return instance;
    }

    /**
     * 添加配置项
     *
     * @param config
     * @return 返回插入的行数
     */
    public long addConfig(BaseConfig config) {
        try {
            return dbHelper.addConfig(config.getName(), config.getValue());
        } catch (Exception e) {
            Log.e("BaseConfigInstance", "Error adding config: " + e.getMessage(), e);
            return -1; // 返回-1表示失败
        }
    }

    /**
     * 更新配置项
     *
     * @param config
     * @return 返回更新的行数
     */
    public int updateConfig(BaseConfig config) {
        try {
            return dbHelper.updateConfig(Long.valueOf(config.getId()), config.getName(), config.getValue());
        } catch (Exception e) {
            Log.e("BaseConfigInstance", "Error updating config: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被更新
        }
    }

    /**
     * 删除配置项
     *
     * @param id 配置项ID
     * @return 返回删除的行数
     */
    public int deleteConfig(long id) {
        try {
            return dbHelper.deleteConfig(id);
        } catch (Exception e) {
            Log.e("BaseConfigInstance", "Error deleting config: " + e.getMessage(), e);
            return 0; // 返回0表示没有行被删除
        }
    }

    /**
     * 获取配置项
     *
     * @param id 配置项ID
     * @return 返回配置项
     */
    public BaseConfig getConfig(long id) {
        try {
            return dbHelper.getConfig(id);
        } catch (Exception e) {
            Log.e("BaseConfigInstance", "Error retrieving config by ID: " + e.getMessage(), e);
            return null; // 返回null表示未找到
        }
    }

    /**
     * 获取所有配置
     *
     * @return List<BaseConfig>
     */
    public List<BaseConfig> getAllConfigs() {
        try {
            return dbHelper.getAllConfigs();
        } catch (Exception e) {
            Log.e("BaseConfigInstance", "Error retrieving all configs: " + e.getMessage(), e);
            return new ArrayList<>(); // 返回空的列表
        }
    }

    /**
     * 获取配置项根据姓名
     *
     * @param name 配置项name
     * @return 返回配置项
     */
    public BaseConfig getConfigByName(String name) {
        try {
            return dbHelper.getConfigByName(name);
        } catch (Exception e) {
            Log.e("BaseConfigInstance", "Error retrieving config by name: " + e.getMessage(), e);
            return null; // 返回null表示未找到
        }
    }

    public void setInstance(BaseConfigHelper baseConfigHelper) {
        this.dbHelper = baseConfigHelper;
    }
}